package com.appointment.validation;

import jakarta.validation.ConstraintValidator;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class AfterTimePeriodValidator implements ConstraintValidator<AfterTimePeriod, String> {
    private long timePeriod;

    @Override
    public void initialize(AfterTimePeriod constraintAnnotation) {
        this.timePeriod = constraintAnnotation.timePeriod();
    }

    @Override
    public boolean isValid(String value, jakarta.validation.ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }

        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(value, formatter);
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime threshold = now.plusSeconds(timePeriod);
            return dateTime.isAfter(threshold);
        } catch (Exception e) {
            return false;
        }
    }

}
